﻿using System;
using System.Linq;

namespace DataAccess.Repository
{
	public class KPIProgressRepository : IKPIProgressRepository, IDisposable
	{
		private KPIDbContext _KPIDbContext = new KPIDbContext();

		#region IDisposable Members

		public void Dispose()
		{
			if(_KPIDbContext != null)
			{
				_KPIDbContext.Dispose();
			}
		}

		#endregion

		#region IKPIProgressRepository Members

		/// <summary>
		/// 保存KPI进度
		/// </summary>
		/// <param name="theKPIProgress"></param>
		/// <returns></returns>
		public bool SaveKPIProgress(Models.KPIProgress theKPIProgress, bool updateFromKPIProgressForUpdate)
		{
			var result = false;

			if(theKPIProgress != null)
			{
				try
				{
					var orgKPIProgress = _KPIDbContext.KPIProgress.Find(theKPIProgress.KPIID);

					if(orgKPIProgress != null)
					{
						theKPIProgress.KPIProgressModifyTime = DateTime.Now;

						if(updateFromKPIProgressForUpdate)
						{
							var theKPIProgressForUpdate = _KPIDbContext.KPIProgressForUpdate.Find(theKPIProgress.KPIID);

							if(theKPIProgressForUpdate != null)
							{
								if(!string.IsNullOrWhiteSpace(theKPIProgressForUpdate.UpdatedQuarter1Progress))
								{
									theKPIProgress.Quarter1Progress = theKPIProgressForUpdate.UpdatedQuarter1Progress;
								}
								else if(!string.IsNullOrWhiteSpace(theKPIProgressForUpdate.UpdatedQuarter2Progress))
								{
									theKPIProgress.Quarter2Progress = theKPIProgressForUpdate.UpdatedQuarter2Progress;
								}
								else if(!string.IsNullOrWhiteSpace(theKPIProgressForUpdate.UpdatedQuarter3Progress))
								{
									theKPIProgress.Quarter3Progress = theKPIProgressForUpdate.UpdatedQuarter3Progress;
								}
								else if(!string.IsNullOrWhiteSpace(theKPIProgressForUpdate.UpdatedQuarter4Progress))
								{
									theKPIProgress.Quarter4Progress = theKPIProgressForUpdate.UpdatedQuarter4Progress;
								}

								_KPIDbContext.KPIProgressForUpdate.Remove(theKPIProgressForUpdate);
							}
						}
						else
						{
							//_KPIDbContext.Entry(orgKPIProgress).CurrentValues.SetValues(theKPIProgress);
							var m = DateTime.Now.Month;

							if(m >= 1 && m <= 3)
							{
								orgKPIProgress.Quarter1Progress = theKPIProgress.Quarter1Progress;
							}
							else if(m >= 4 && m <= 6)
							{
								orgKPIProgress.Quarter2Progress = theKPIProgress.Quarter2Progress;
							}
							else if(m >= 7 && m <= 9)
							{
								orgKPIProgress.Quarter3Progress = theKPIProgress.Quarter3Progress;
							}
							else if(m >= 10 && m <= 12)
							{
								orgKPIProgress.Quarter4Progress = theKPIProgress.Quarter4Progress;
							}
						}
					}
					else
					{
						_KPIDbContext.KPIProgress.Add(theKPIProgress);
					}

					if(_KPIDbContext.SaveChanges() > 0)
					{
						result = true;
					}
				}
				catch
				{
					throw;
				}
			}

			return result;
		}

		/// <summary>
		/// 保存更新的KPI进度
		/// </summary>
		/// <param name="lstKPIProgressForUpdate"></param>
		/// <returns></returns>
		public bool SaveKPIProgressForUpdate(Models.KPIProgressForUpdate theKPIProgressForUpdate)
		{
			var result = false;

			if(theKPIProgressForUpdate != null)
			{
				try
				{
					var orgKPIProgressForUpdate = _KPIDbContext.KPIProgressForUpdate.Find(theKPIProgressForUpdate.KPIID);

					if(orgKPIProgressForUpdate != null)
					{
						_KPIDbContext.Entry(orgKPIProgressForUpdate).CurrentValues.SetValues(theKPIProgressForUpdate);
					}
					else
					{
						_KPIDbContext.KPIProgressForUpdate.Add(theKPIProgressForUpdate);
					}

					if(_KPIDbContext.SaveChanges() > 0)
					{
						result = true;
					}
				}
				catch
				{
					throw;
				}
			}

			return result;
		}

		///// <summary>
		///// 删除修改的KPI进度
		///// </summary>
		///// <param name="taskID"></param>
		///// <returns></returns>
		//public bool DeleteKPIProgressForUpdateByTask(int taskID)
		//{
		//  var result = false;

		//  if(taskID > 0)
		//  {
		//    try
		//    {
		//      var kpiIDs = _KPIDbContext.KPI.Where(kpi => kpi.TaskID == taskID).Select(kpi => kpi.KPIID);

		//      if(kpiIDs != null)
		//      {
		//        var kpiProgressForUpdate = _KPIDbContext.KPIProgressForUpdate.Where(kpipu => kpiIDs.Contains(kpipu.KPIID));

		//        if(kpiProgressForUpdate != null)
		//        {
		//          var lstKPIProgressForUpdate = kpiProgressForUpdate.ToList();

		//          for(var i = 0; i < lstKPIProgressForUpdate.Count; i++)
		//          {
		//            _KPIDbContext.KPIProgressForUpdate.Remove(lstKPIProgressForUpdate[i]);
		//          }

		//          if(_KPIDbContext.SaveChanges() > 0)
		//          {
		//            result = true;
		//          }
		//        }
		//      }
		//    }
		//    catch
		//    {
		//      throw;
		//    }
		//  }

		//  return result;
		//}

		///// <summary>
		///// 更新KPI进度
		///// </summary>
		///// <returns></returns>
		//public bool UpdateKPIProgress(int taskID)
		//{
		//  var result = false;

		//  try
		//  {
		//    var kpiIDs = _KPIDbContext.KPI.Where(kpi => kpi.TaskID == taskID).Select(kpi => kpi.KPIID);

		//    if(kpiIDs != null)
		//    {
		//      var kpiProgressForUpdate = _KPIDbContext.KPIProgressForUpdate.Where(kpipu => kpiIDs.Contains(kpipu.KPIID));
		//      var kpiProgress = _KPIDbContext.KPIProgress.Where(kpipu => kpiIDs.Contains(kpipu.KPIID));

		//      if(kpiProgressForUpdate != null && kpiProgress != null)
		//      {
		//        var lstKPIProgressForUpdate = kpiProgressForUpdate.ToList();

		//        for(var i = 0; i < lstKPIProgressForUpdate.Count; i++)
		//        {
		//          var theKPIProgress = kpiProgress.FirstOrDefault(kpip => kpip.KPIID == lstKPIProgressForUpdate[i].KPIID);

		//          if(theKPIProgress != null)
		//          {
		//            if(!string.IsNullOrWhiteSpace(lstKPIProgressForUpdate[i].UpdatedQuarter1Progress))
		//            {
		//              theKPIProgress.Quarter1Progress = lstKPIProgressForUpdate[i].UpdatedQuarter1Progress;
		//            }
		//            if(!string.IsNullOrWhiteSpace(lstKPIProgressForUpdate[i].UpdatedQuarter2Progress))
		//            {
		//              theKPIProgress.Quarter2Progress = lstKPIProgressForUpdate[i].UpdatedQuarter2Progress;
		//            }
		//            if(!string.IsNullOrWhiteSpace(lstKPIProgressForUpdate[i].UpdatedQuarter3Progress))
		//            {
		//              theKPIProgress.Quarter3Progress = lstKPIProgressForUpdate[i].UpdatedQuarter3Progress;
		//            }
		//            if(!string.IsNullOrWhiteSpace(lstKPIProgressForUpdate[i].UpdatedQuarter4Progress))
		//            {
		//              theKPIProgress.Quarter4Progress = lstKPIProgressForUpdate[i].UpdatedQuarter4Progress;
		//            }

		//            theKPIProgress.KPIProgressModifyTime = DateTime.Now;
		//          }

		//          _KPIDbContext.KPIProgressForUpdate.Remove(lstKPIProgressForUpdate[i]);
		//        }

		//        if(_KPIDbContext.SaveChanges() > 0)
		//        {
		//          result = true;
		//        }
		//      }
		//    }
		//  }
		//  catch
		//  {
		//    throw;
		//  }

		//  return result;
		//}

		/// <summary>
		/// 删除更新的KPI进度
		/// </summary>
		/// <param name="kpiid"></param>
		/// <returns></returns>
		public bool DeleteKPIProgressForUpdate(int kpiid)
		{
			var result = false;

			if(kpiid > 0)
			{
				var theKPIProgressUpdate = _KPIDbContext.KPIProgressForUpdate.Find(kpiid);

				if(theKPIProgressUpdate != null)
				{
					_KPIDbContext.KPIProgressForUpdate.Remove(theKPIProgressUpdate);

					result = (_KPIDbContext.SaveChanges() > 0);
				}
			}

			return result;
		}

		/// <summary>
		/// 根据单位取KPI进度
		/// </summary>
		/// <param name="groupID"></param>
		/// <returns></returns>
		public System.Collections.Generic.List<Models.KPIProgress> GetKPIProgressByGroup(int groupID, int y)
		{
			System.Collections.Generic.List<Models.KPIProgress> result = null;

			if(groupID > 0)
			{
				try
				{
					var kpiids = from kpi in _KPIDbContext.KPI
											 where kpi.GroupID == groupID
											 select kpi.KPIID;

					var progress = from prg in _KPIDbContext.KPIProgress
												 join kpi in _KPIDbContext.KPI on prg.KPIID equals kpi.KPIID into t1
												 from prg_kpi in t1.DefaultIfEmpty()
												 where prg_kpi.GroupID == groupID && prg_kpi.TheYear == y
												 select new
												 {
													 KPIID = prg_kpi.KPIID,
													 KPIProgressAddTime = prg.KPIProgressAddTime,
													 KPIProgressModifyTime = prg.KPIProgressModifyTime,
													 Quarter1Progress = prg.Quarter1Progress,
													 Quarter2Progress = prg.Quarter2Progress,
													 Quarter3Progress = prg.Quarter3Progress,
													 Quarter4Progress = prg.Quarter4Progress,
													 EndOfTheYearAssessment = prg.EndOfTheYearAssessment,
													 KPIProgressState = prg.KPIProgressState,
													 KPIProgressRemark = prg.KPIProgressRemark,
													 ActionPlans = prg_kpi.ActionPlans,
													 CurrentState = prg_kpi.CurrentState,
													 FinishTime = prg_kpi.FinishTime,
													 GroupID = prg_kpi.GroupID,
													 KPIAddTime = prg_kpi.KPIAddTime,
													 KPIContent = prg_kpi.KPIContent,
													 KPIModifyTime = prg_kpi.KPIModifyTime,
													 PreState = prg_kpi.PreState,
													 ResponsablePerson = prg_kpi.ResponsablePerson,
													 ResponseDepartment = prg_kpi.ResponseDepartment,
													 ResponsibleLeadership = prg_kpi.ResponsibleLeadership,
													 RLTel = prg_kpi.RLTel,
													 RPTel = prg_kpi.RPTel,
													 StartTime = prg_kpi.StartTime,
													 TheYear = prg_kpi.TheYear
												 };

					if(progress != null)
					{
						result = new System.Collections.Generic.List<Models.KPIProgress>();

						foreach(var item in progress)
						{
							var theKPIProgress = new Models.KPIProgress()
							{
								KPIID = item.KPIID,
								EndOfTheYearAssessment = item.EndOfTheYearAssessment,
								KPIProgressRemark = item.KPIProgressRemark,
								KPIProgressState = item.KPIProgressState,
								Quarter1Progress = item.Quarter1Progress,
								Quarter2Progress = item.Quarter2Progress,
								Quarter3Progress = item.Quarter3Progress,
								Quarter4Progress = item.Quarter4Progress,
								KPIProgressAddTime = item.KPIProgressAddTime,
								KPIProgressModifyTime = item.KPIProgressModifyTime,
								TheKPI = new Models.KPI()
								{
									ActionPlans = item.ActionPlans,
									TheYear = item.TheYear,
									StartTime = item.StartTime,
									RPTel = item.RPTel,
									RLTel = item.RLTel,
									ResponsibleLeadership = item.ResponsibleLeadership,
									ResponseDepartment = item.ResponseDepartment,
									ResponsablePerson = item.ResponsablePerson,
									PreState = item.PreState,
									KPIModifyTime = item.KPIModifyTime,
									KPIContent = item.KPIContent,
									CurrentState = item.CurrentState,
									FinishTime = item.FinishTime,
									GroupID = item.GroupID,
									KPIAddTime = item.KPIAddTime,
									KPIID = item.KPIID
								}
							};

							result.Add(theKPIProgress);
						}
					}
				}
				catch
				{
					throw;
				}
			}

			return result;
		}

		#endregion
	}
}
